#ifndef HEAD_H
#define HEAD_H
#include<iostream>
using namespace std;

struct node {
    node*next;
    int data;
};


class arraylist{
private:
    node*head;
    node*tail;
    int length=0;
public:
    arraylist();
    void length_input();
    void creatlist(int i);
    void showlist();
    void inverselist();
    ~arraylist();
};

arraylist::~arraylist(){
    node*current;
    while(head){
        current=head;
        head=head->next;
        delete current;
    }
    head=tail=NULL;
}

arraylist::arraylist(){
    head=tail=NULL;
    length_input();
}


void arraylist::length_input(){
    cout<<"please input the length of arraylist:";
    cin>>length;
        if(length){
            creatlist(length);
        }
        else{
            length_input();
        }
}

void arraylist::creatlist(int i){
    int counter=0;
    node* current=head=new node;
    cout<<"input elements:";
    while(counter<i){
        cin>>current->data;
        counter++;
        if(counter!=i){
            current=current->next=new node;
        }

    }
    tail=current;
    tail->next=NULL;
}

void arraylist::showlist(){
    node*current=head;
    cout<<endl;
    while(current!=NULL){
        cout<<current->data<<"\t";
        current=current->next;
    }
}

void arraylist::inverselist(){

    node*p=NULL,*q=NULL;
    p=head->next;
    head->next=NULL;
    while(p!=NULL){
        q = p->next; //保存p指针指向的下一个节点
        p->next = head;//把p指向的节点放到开头
        head = p;
        p = q;  //p指向q
    }
}

#endif // HEAD_H
